package com.quick.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author jojo.wang
 * @title: MasterDataSourceConfig
 * @projectName spring-boot-quick
 * @description: TODO
 * @date 2019/5/514:58
 */
@Configuration
@EnableConfigurationProperties
@Slf4j
public class MasterDataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.master")
    public DataSourceProperties masterDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Bean
    public DataSource masterDataSource(){
        DataSourceProperties dataSourceProperties = masterDataSourceProperties();
        log.info("masterDataSource is {}",dataSourceProperties.getUrl());
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    @Resource
    public PlatformTransactionManager masterPlatformTransactionManager(DataSource masterDataSource){
        return new DataSourceTransactionManager(masterDataSource);
    }

}
